import threading


def synchronize(func):
    lock = threading.RLock()

    def synced_func(*args, **kws):
        with lock:
            return func(*args, **kws)

    return synced_func


def singleton(cls):
    instance_dict = {}

    @synchronize
    def wrap(*args, **kwargs):
        if instance_dict.get(cls):
            print('返回单例')
            return instance_dict.get(cls)
        else:
            instance = cls(*args, **kwargs)
            instance_dict[cls] = instance
            return instance
    return wrap
